#
# Copyright xgliu515@gmail.com
#


all: $(NK_RAMDISK) build

SRCS := $(wildcard $(SRCS))
OBJS := $(patsubst %.c, %.o, $(SRCS))
SUB_TARGET := .build_in.o

$(OBJS): %.o: %.c
	$(CC) $(NK_BUILD_FLAGS) -c $<

ifneq ($(SUBDIR), )
SUB_OBJS:=$(addsuffix /$(SUB_TARGET),$(SUBDIR))
$(SUB_OBJS):
	for d in $(SUBDIR); do \
		$(MAKE) -C $$d; \
	done;
endif

.PHONY: objs
objs: $(OBJS) $(SUB_OBJS)

$(NK_RAMDISK):
	@echo ">>>>>>>> Please run 'make osp' in top dir, first! <<<<<<<<"; false


#
# Define oss build rule
#

ifeq ($(TYPE), oss)

.PHONY: build
build: .$(DIR)
	$(BUILD) 
	
.$(DIR): $(SOURCE)
	$(DECOMPRESS) $(SOURCE)
	touch .$(DIR)
	
$(SOURCE):
	$(RM) $(SOURCE).*; $(DOWNLOAD_CMD) $(URL)

.PHONY: install
install:
	$(INSTALL) 

.PHONY: clean
clean:
	$(CLEAN)

endif

#
# Define abs build rule
#

ifeq ($(TYPE), abs)

.PHONY: build
build: .$(DIR)
	$(NK_TOOLS)/makepkg --config $(NK_TOOLS)/makepkg.conf -f

.PHONY: install
install:
	for i in $(BIN); do \
		bin=`find pkg/ -name "$$i" | sed 's/pkg\///'`; \
		cp pkg/$$bin $(NK_RAMDISK)/$$bin; \
		$(NK_TOOLS)/findlib.sh $(NK_RAMDISK) pkg/$$bin; \
	done
	$(INSTALL) 

.PHONY: clean
clean:
	$(CLEAN)

endif



#
# Define obj build rule
#

ifeq ($(TYPE), obj)

ifeq ($(STYPE), lib)
NK_CFLAGS += -fPIC --shared
endif

.PHONY: build
build: $(OBJS) $(SUB_OBJS)
ifeq ($(STYPE), lib)
	@$(LD) $(OBJS) -o $(SUB_TARGET)
else
	@$(LD) $(OBJS) -o $(SUB_TARGET)
endif

.PHONY: install
install: 


.PHONY: clean
clean:
	$(RM) $(OBJS) $(SUB_TARGET)
ifneq ($(SUBDIR), )
	for d in $(SUBDIR); do \
		$(MAKE) -C $$d clean; \
	done;
endif

endif


#
# Define bin build rule
#

ifeq ($(TYPE), bin)

NK_BUILD_FLAGS += $(NK_LIB)

.PHONY: build
build: $(OBJS) $(SUB_OBJS)
	$(CC) $(NK_BUILD_FLAGS) $(OBJS) $(SUB_OBJS) -o $(TARGET) *.c 

.PHONY: install
install: 
	$(MKDIR) $(NK_RAMDISK)/nkfm/bin
	$(INSTALL) $(TARGET) $(NK_RAMDISK)/nkfm/bin/
	$(NK_TOOLS)/findlib.sh $(NK_RAMDISK) $(TARGET) 

.PHONY: clean
clean:
	$(RM) $(TARGET)
ifneq ($(SUBDIR), )
	for d in $(SUBDIR); do \
		$(MAKE) -C $$d $@; \
	done;
endif

endif


#
# Define lib build rule
#

ifeq ($(TYPE), lib)

NK_BUILD_FLAGS += -fPIC --shared 
LIBTARGET := lib$(TARGET).so

.PHONY: build
build: objs
	$(CC) $(NK_BUILD_FLAGS) $(OBJS) $(SUB_OBJS) -o $(LIBTARGET) 
	$(LN) `pwd`/$(LIBTARGET) $(NK_SRC)/lib/

.PHONY: install
install:
	$(MKDIR) $(NK_RAMDISK)/nkfm/lib 
	$(INSTALL) $(LIBTARGET) $(NK_RAMDISK)/nkfm/lib/
	$(NK_TOOLS)/findlib.sh $(NK_RAMDISK) $(LIBTARGET)

.PHONY: clean
clean: 
	$(RM) $(LIBTARGET) $(NK_SRC)/lib/$(LIBTARGET) $(OBJS) $(SUB_OBJS)
ifneq ($(SUBDIR), )
	for d in $(SUBDIR); do \
		$(MAKE) -C $$d $@; \
	done;
endif

endif


#
# Define directoty build rule
#

ifeq ($(TYPE), dir)
.PHONY: build
build: 
	for d in $(SUBDIR); do \
		$(MAKE) -C $$d; \
	done; 

.PHONY: clean
clean:
	for d in $(SUBDIR); do \
		$(MAKE) -C $$d $@; \
	done; 

.PHONY: install
install:
	for d in $(SUBDIR); do \
		$(MAKE) -C $$d $@; \
	done; 

endif


#
# Define file build rule
#

ifeq ($(TYPE), file)
.PHONY: build
build: 

.PHONY: clean
clean:

.PHONY: install
install:
	$(INSTALL)

endif


#
# Define kernel module build rule
#

ifeq ($(TYPE), module)

EXTRA_CFLAGS += $(DEBFLAGS) -I$(NK_INCDIR) $(NK_CFLAGS) # -D"KBUILD_STR(s)=\#s" 
#KERNELDIR := /lib/modules/$(shell uname -r)/build
KERNELDIR ?= $(NK_KERNEL)
PWD := $(shell pwd)

build:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
	$(RM) *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.order *.symvers

depend .depend dep:
	$(CC) $(CFLAGS) -M *.c > .depend

install:
	$(MKDIR) $(NK_RAMDISK)/nkfm/modules
	$(INSTALL) *.ko $(NK_RAMDISK)/nkfm/modules/

ifeq (.depend,$(wildcard .depend))
include .depend
endif

endif

